package com.heima.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.logging.log4j.util.Strings;
import org.hibernate.validator.internal.constraintvalidators.bv.time.pastorpresent.AbstractPastOrPresentEpochBasedValidator;
import org.springframework.stereotype.Service;
import springfox.documentation.service.ApiListing;

import java.util.Date;

/**
 * @author lo
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel>
        implements AdChannelService {

    /**
     * 根据频道名称分页查询频道列表
     *
     * @param channelDto
     * @return
     */
    @Override
    public ResponseResult findChannelbyPage(ChannelDto channelDto) {
        //检验参数
        if (channelDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //1.检查分页参数
        channelDto.checkParam();
        //2.查看传递的name名称是否为空  拼接查询条件
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();
        if (Strings.isNotBlank(channelDto.getName())) {
            //不为空 根据name模糊查询
            wrapper.like(AdChannel::getName, channelDto.getName());
        }
        //根据id排序
        wrapper.orderByAsc(AdChannel::getOrd);
        //3.封装分页参数
        IPage<AdChannel> page = new Page<>(channelDto.getPage(), channelDto.getSize());
        //4.进行分页查询
        page(page, wrapper);

        //5.封装返回结果
        PageResponseResult result = new PageResponseResult(channelDto.getPage(), channelDto.getSize(), page.getTotal(), page.getRecords());
        //返回结果
        return result;
    }

    /**
     * 根据id删除频道信息
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delChannel(Integer id) {
        //1.校验参数
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.查询这个频道是否存在
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getId, id));
        if (count == 0) {
            //说明该频道不存在
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST);
        }
        //3.根据id删除频道
        removeById(id);
        return ResponseResult.okResult();
    }

    /**
     * 根据id新增频道信息
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(AdChannel adChannel) {
        //注意:  id是自动生成的所以加不进去
        //1.校验参数
        if (adChannel == null || Strings.isBlank(adChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.判断频道是否已经存在
        int count = count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
        if (count > 0) {
            //说明频道已经存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道已经存在,不可重复添加");
        }
        //3.添加频道
        adChannel.setCreated_time(new Date());
        save(adChannel);
        return ResponseResult.okResult();

    }

    /**
     * 修改频道信息
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {
        //1. 校验参数
        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //2 执行修改
        AdChannel channel = getById(adChannel.getId());
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "频道信息不存在");
        }

        //3.校验名称唯一性
        //判断
        if (Strings.isNotBlank(channel.getName()) && !channel.getName().equals(adChannel.getName())) {
            //说明查询到的频道名称不为空,并且 查询到的名称和传递的名称不相同
            int count = count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getId, adChannel.getId()));
            if (count == 0) {
                //说明频道不存在
                return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST);
            }
        }
        //说明频道存在 可以修改
        updateById(adChannel);
        return ResponseResult.okResult();

    }
}




